﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace HQWeb.LibarySys
{
    public partial class ViewOutApply : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                if (Session["USER_NAME"] != null)
                {
                    Approver.Text = Session["USER_NAME"].ToString();
                }
                if (Request["sign"] != null)
                {
                    if (Request["sign"] == "1")
                    {
                        btnPass.Visible = true;
                        btnReject.Visible = true;
                        btnReturnA.Visible = false;
                        btnReturnE.Visible = true;
                        btnReturnO.Visible = false;
                    }
                    else if (Request["sign"] == "2")
                    {
                        btnPass.Visible = true;
                        btnReturnA.Visible = false;
                        btnReturnE.Visible = true;
                        btnReturnO.Visible = false;
                    }
                    else if (Request["sign"] == "3")
                    {
                        btnPass.Visible = false;
                        btnReturnA.Visible = true;
                        btnReturnE.Visible = false;
                        btnReturnO.Visible = false;
                        MyNotation.ReadOnly = true;
                    }
                    else if (Request["sign"] == "4")
                    {
                        btnPass.Visible = true;
                        btnReturnA.Visible = false;
                        btnReturnE.Visible = false;
                        btnReturnO.Visible = true;
                        Remark.ReadOnly = false;
                        MyNotation.ReadOnly = true;
                    }
                    else if (Request["sign"] == "5")
                    {
                        btnPass.Visible = false;
                        btnReturnA.Visible = false;
                        btnReturnE.Visible = false;
                        btnReturnO.Visible = true;
                        MyNotation.ReadOnly = true;
                    }
                    else
                    {
                        btnReturnA.Visible = false;
                        btnReturnE.Visible = false;
                        MyNotation.ReadOnly = true;
                        btnReturnO.Visible = true;
                    }
                }
                else
                {
                    MyNotation.ReadOnly = true;
                }
                if (Request["id"] != null)
                {
                    string id = Request["id"];
                    BindDate(id);
                }

            }

        }

        #region  绑定数据
        private void BindDate(string id)
        {
            Model.LibrarySys.T_PUB_LIBRARYOUT libraryout = new Model.LibrarySys.T_PUB_LIBRARYOUT();
            BLL.LibrarySys.T_PUB_LIBRARYOUT bll = new BLL.LibrarySys.T_PUB_LIBRARYOUT();
            libraryout = bll.GetModel(id);
            int branch_id = int.Parse(libraryout.BRANCH_ID.ToString());
            BindBranchName(branch_id);
            int library_id = int.Parse(libraryout.LIBRARY_ID.ToString());
            BindLibraryName(library_id);
            //Applications.Text = libraryout.NUM.ToString();
            //int material_id = int.Parse(libraryout.MATERIAL_ID.ToString());
            BindMaterial(id);
            Applicants.Text = libraryout.APPLICANT_NAME;
            if (libraryout.APPLY_TIMEOUT != null)
            {
                begintime.Text = Convert.ToDateTime(libraryout.APPLY_TIMEOUT).ToShortDateString();
            }
            if (libraryout.PLAN_TIMEIN != null)
            {
                endtime.Text = Convert.ToDateTime(libraryout.PLAN_TIMEIN).ToShortDateString();
            }
            Remark.Text = libraryout.REMARK;
            Titel.Text = libraryout.TITEL;
            if (libraryout.APPROVER_NAME != null && libraryout.APPROVER_NAME != "")
            {
                Approver.Text = libraryout.APPROVER_NAME;
            }
            MyNotation.Text = libraryout.NOTATION;
        }
        #endregion

        //#region 绑定审批人
        //private void BindApprover(int id)
        //{
        //    Model.SysManage.T_PUB_USER user = new Model.SysManage.T_PUB_USER();
        //    BLL.SysManage.T_PUB_USER bll = new BLL.SysManage.T_PUB_USER();
        //    user = bll.GetModel(id);
        //    Approver.Text = user.NAME;
        //}
        //#endregion

        #region 绑定材料
        private void BindMaterial(string id)
        {
            BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL bll = new BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
            DataTable modelList = bll.GetListByPid(id).Tables[0];
            StringBuilder strHtml = new StringBuilder();
            int count = modelList.Rows.Count;
            string strEID;
            string strMateral;
            string strMateralName;
            string strNum;

            for (int i = 0; i < count; i++)
            {
                strEID = modelList.Rows[i]["ID"].ToString();
                strMateral = modelList.Rows[i]["MATERIAL_ID"].ToString();
                strMateralName = modelList.Rows[i]["MATERIAL_NAME"].ToString();
                strNum = modelList.Rows[i]["NUM"].ToString();

                strHtml.Append("  <table width=\"58%\" id=\"table" + i + "\">");
                strHtml.Append(" <tr>");
                strHtml.Append("<td align=\"right\" style=\"width:10%\">");
                strHtml.Append("元素名称：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width:10%\">");
                strHtml.Append(" <input type=\"text\" readonly=\"readonly\" id=\"n" + i + "\" onchange=\"this.setAttribute('value',this.value);\" value=\"" + strMateralName + "\"/>");
                strHtml.Append("</td>");
                strHtml.Append("<td style=\"width: 10%\" align=\"right\">");
                strHtml.Append("申请数量：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width: 10%\">");
                strHtml.Append("<input type=\"text\" readonly=\"readonly\" onchange=\"this.setAttribute('value',this.value);\" id=\"s" + i + "\" value=\"" + strNum + "\"/>");
                strHtml.Append(" </td>");
                //strHtml.Append("<td style=\"width: 10%\" align=\"right\">");
                //strHtml.Append("库存数量：");
                //strHtml.Append("</td>");
                //strHtml.Append("<td align=\"left\">");
                //strHtml.Append("<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" readonly=\"readonly\" id=\"c" + i + "\" value=\"" + strNum + "\"/>");
                //strHtml.Append(" </td>");
                //strHtml.Append("<td align=\"right\" style=\"width: 10%\" >");
                //strHtml.Append("<input type=\"button\" id=\"del" + i + "\" value=\"删除\"  onclick=\"DeleteNode('table" + i + "')\"/>");
                //strHtml.Append("</td>");
                strHtml.Append(" <input type=\"text\" id=\"p" + i + "\" style=\"display: none\" value=\"" + strMateral + "\"/>");
                strHtml.Append(" <input type=\"text\" id=\"e" + i + "\" style=\"display: none\" value=\"" + strEID + "\" />");
                strHtml.Append(" </tr>");
                strHtml.Append(" </table>");
            }
            Addtable.InnerHtml = strHtml.ToString();
            hidCount.Value = count.ToString();
        }
        #endregion

        #region 绑定仓库
        private void BindLibraryName(int library_id)
        {
            BLL.SysManage.T_PUB_LIBRARY bll = new BLL.SysManage.T_PUB_LIBRARY();
            Model.SysManage.T_PUB_LIBRARY library = bll.GetModel(library_id);
            if (library != null)
            {
                this.LibrayName.Text = library.LIBRARY_NAME;
                //this.LibraryID.Value = library.LIBRARY_ID.ToString();
            }
        }
        #endregion

        #region 绑定分店列表
        private void BindBranchName(int branch_id)
        {
            Model.SysManage.T_PUB_BRANCH branch = new Model.SysManage.T_PUB_BRANCH();
            BLL.SysManage.T_PUB_BRANCH LibraryBll = new BLL.SysManage.T_PUB_BRANCH();
            branch = LibraryBll.GetModel(branch_id);
            BranchName.Text = branch.BRANCH_NAME;
        }
        #endregion
      
        protected void btnPass_Click(object sender, EventArgs e)
        {
            if (Request["sign"] == "4")
            {
                string id = Request["id"];
                Model.LibrarySys.T_PUB_LIBRARYOUT libraryout = new Model.LibrarySys.T_PUB_LIBRARYOUT();
                BLL.LibrarySys.T_PUB_LIBRARYOUT lbll = new BLL.LibrarySys.T_PUB_LIBRARYOUT();
                libraryout = lbll.GetModel(id);
                libraryout.STATE = "4";
                libraryout.REMARK = Remark.Text;
                libraryout.REAL_TIMEOUT = DateTime.Now;
                lbll.Update(libraryout);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "出库申请" + libraryout.TITEL + " 状态为已出库" , DateTime.Now);
                Response.Redirect("OutLibrary.aspx");
                //Model.LibrarySys.T_PUB_LIBRARYMATERIAL material = new Model.LibrarySys.T_PUB_LIBRARYMATERIAL();
            }
            else
            {
                bool flag = true;
                string itemName = "";
                int num = 0;
                Model.LibrarySys.T_PUB_LIBRARYOUT model = new Model.LibrarySys.T_PUB_LIBRARYOUT();
                BLL.LibrarySys.T_PUB_LIBRARYOUT bll = new BLL.LibrarySys.T_PUB_LIBRARYOUT();
                string id = Request["id"];
                model = bll.GetModel(id);
                model.STATE = "3";
                model.NOTATION = MyNotation.Text;
                model.APPROVER_NAME = Session["USER_NAME"].ToString();
                model.APPROVER = int.Parse(Session["USER_ID"].ToString());
                int branch_id = int.Parse(model.LIBRARY_ID.ToString());

                BLL.LibrarySys.T_PUB_LIBRARYMATERIAL mbbl = new BLL.LibrarySys.T_PUB_LIBRARYMATERIAL();
                IList<Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL> materialList = new List<Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL>();
                BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL obll = new BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
                materialList = obll.GetModelList(id);
                List<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> MList = mbbl.GetListArrayByBranch(branch_id);
                List<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> UpdateList = new List<Model.LibrarySys.T_PUB_LIBRARYMATERIAL>();
                foreach(Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL item in materialList)
                {
                    IEnumerable<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> lo = MList.Where(p => p.MATERIAL_ID == item.MATERIAL_ID);
                    if (lo.Count<Model.LibrarySys.T_PUB_LIBRARYMATERIAL>() >= 1)
                    {
                        if (lo.ElementAt(0).NUM >= item.NUM)
                        {
                            lo.ElementAt(0).NUM -= item.NUM;
                            UpdateList.Add(lo.ElementAt(0));
                        }
                        else
                        {
                            //Universal.JS.Alert(item.MATERIAL_NAME + "库存量不足！请您及时采购！");
                            flag = false;
                            itemName = item.MATERIAL_NAME;
                            num = (int)lo.ElementAt(0).NUM; 
                            break;
                        }
                    }
                    else
                    {
                        //Universal.JS.Alert("仓库不存在" + item.MATERIAL_NAME + "！请您及时采购！");
                        flag = false;
                        itemName = item.MATERIAL_NAME;
                        break;
                    }
                }
                if (flag)
                {
                    mbbl.UpdateLibraryMaterial(model, UpdateList);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "出库申请" + model.TITEL + " 状态为已审批", DateTime.Now);
                    //bll.Update(model);
                    //bll.UpdatePass(id, "3", MyNotation.Text, int.Parse(Session["USER_ID"].ToString()), Session["USERE_NAME"].ToString());
                    Response.Redirect("ExamineApply.aspx");
                }
                else
                {
                    Universal.JS.Alert("仓库中缺少" + itemName + "！现存数量为：" + num + "！请您及时采购！");
                }
                //ErrorMessage.Text = "已成功审批！";
                //btnPass.Enabled = false;
                //btnReject.Enabled = false;
            }
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            Model.LibrarySys.T_PUB_LIBRARYOUT model = new Model.LibrarySys.T_PUB_LIBRARYOUT();
            BLL.LibrarySys.T_PUB_LIBRARYOUT bll = new BLL.LibrarySys.T_PUB_LIBRARYOUT();
            string id = Request["id"];
            model = bll.GetModel(id);
            model.STATE = "2";
            model.NOTATION = MyNotation.Text;
            model.APPROVER_NAME = Session["USER_NAME"].ToString();
            model.APPROVER = int.Parse(Session["USER_ID"].ToString());
            bll.Update(model);
            Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "出库申请" + model.TITEL + " 状态为审批不通过", DateTime.Now);
            //bll.UpdatePass(id, "2", MyNotation.Text, int.Parse(Session["USER_ID"].ToString()), Session["USERE_NAME"].ToString());
            Response.Redirect("ExamineApply.aspx");
            //ErrorMessage.Text = "已成功驳回！";
            //btnPass.Enabled = false;
            //btnReject.Enabled = false;
        }

        protected void btnReturnA_Click(object sender, EventArgs e)
        {
            Response.Redirect("ApplyList.aspx");
        }

        protected void btnReturnE_Click(object sender, EventArgs e)
        {
            Response.Redirect("ExamineApply.aspx");
        }

        protected void btnReturnO_Click(object sender, EventArgs e)
        {
            Response.Redirect("OutLibrary.aspx");
        }
    }
}